namespace com.stifel.SharePoint
{
    using System;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using Microsoft.SharePoint.Security;
    using Microsoft.Win32;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid( "790d49c4-b4ac-47ca-baf3-49b10aecd0f8" )]
    public class CustomLoggingFeatureEventReceiver : SPFeatureReceiver
    {
        #region Methods

        public override void FeatureActivated( SPFeatureReceiverProperties properties )
        {
            registerLoggingService( properties );
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.
        public override void FeatureDeactivating( SPFeatureReceiverProperties properties )
        {
            unregisterLoggingService( properties );
        }

        void registerLoggingService( SPFeatureReceiverProperties properties )
        {
            SPFarm spFarm = properties.Definition.Farm;
            if ( spFarm != null )
            {
                CustomLoggingService service = CustomLoggingService.Local;
                if ( service == null )
                {
                    service = new CustomLoggingService( );
                    service.Uncache( );
                    service.Unprovision( );
                    service.Update( true );
                    if ( service.Status != SPObjectStatus.Online )
                        service.Provision( );
                }
                foreach ( SPServer server in spFarm.Servers.Where( a => a.Role != SPServerRole.Invalid ) )
                {
                    RegistryKey basekey = RegistryKey.OpenRemoteBaseKey( RegistryHive.LocalMachine, server.Address );
                    if ( basekey != null )
                    {
                        RegistryKey eventlogkey = basekey.OpenSubKey( CustomLoggingService.EventLog.ApplicationRegistryKeyPath, true );
                        if ( eventlogkey != null )
                        {
                            RegistryKey logginservicekey = eventlogkey.OpenSubKey( CustomLoggingService.EventLog.ApplicationRegistrySubkeyName );
                            if ( logginservicekey == null )
                            {
                                logginservicekey = eventlogkey.CreateSubKey( CustomLoggingService.EventLog.ApplicationRegistrySubkeyName, RegistryKeyPermissionCheck.ReadWriteSubTree );
                                logginservicekey.SetValue( "EventMessageFile", @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\EventLogMessages.dll", RegistryValueKind.String );
                            }
                        }
                    }
                }
            }
        }

        void unregisterLoggingService( SPFeatureReceiverProperties properties )
        {
            SPFarm spFarm = properties.Definition.Farm;
            if ( spFarm != null )
            {
                CustomLoggingService service = CustomLoggingService.Local;
                if ( service != null )
                    service.Delete( );
                foreach ( SPServer server in spFarm.Servers.Where( a => a.Role != SPServerRole.Invalid ) )
                {
                    RegistryKey basekey = RegistryKey.OpenRemoteBaseKey( RegistryHive.LocalMachine, server.Address );
                    if ( basekey != null )
                    {
                        RegistryKey eventlogkey = basekey.OpenSubKey( CustomLoggingService.EventLog.ApplicationRegistryKeyPath, true );
                        if ( eventlogkey != null )
                        {
                            RegistryKey loggingservicekey = eventlogkey.OpenSubKey( CustomLoggingService.EventLog.ApplicationRegistrySubkeyName );
                            if ( loggingservicekey != null )
                                eventlogkey.DeleteSubKey( CustomLoggingService.EventLog.ApplicationRegistrySubkeyName );
                        }
                    }
                }
            }
        }

        #endregion Methods

        #region Other

        // Uncomment the method below to handle the event raised after a feature has been installed.
        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}
        // Uncomment the method below to handle the event raised before a feature is uninstalled.
        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}
        // Uncomment the method below to handle the event raised when a feature is upgrading.
        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

        #endregion Other
    }
}